package com.single.weblearn.service.impl;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.single.weblearn.dao.ProductDao;
import com.single.weblearn.dao.TrxDao;
import com.single.weblearn.json.Buy;
import com.single.weblearn.meta.Trx;
import com.single.weblearn.service.ProductWebService;

@Service("productWebService")
public class ProductWebServiceImpl implements ProductWebService {
	@Autowired
	private TrxDao trxdao;
	@Autowired
	private ProductDao productdao;

	@Transactional
	@Override
	public void buyProduct(List<Buy> list) {
	 
	   List<Trx> list_s = new ArrayList<Trx>();
	   List<Trx> list_b = new ArrayList<Trx>();
	   BigInteger time = BigInteger.valueOf(System.currentTimeMillis());
	   
		for(Buy buy:list){
		Trx trx_sell=new Trx();
	    Trx trx_buy=new Trx();
		Double price = productdao.getPrice(buy.getId());
		
		trx_sell.setContentId(buy.getId());
		trx_buy.setContentId(buy.getId());
		trx_sell.setPersonId(1);
		trx_buy.setPersonId(0);
		trx_sell.setPrice(price);
		trx_buy.setPrice(price);
		trx_sell.setTime(time);
		trx_buy.setTime(time);
		
		list_s.add(trx_sell);
		list_b.add(trx_buy);
	   }
		trxdao.insertTrxList(list_b);
		trxdao.insertTrxList(list_s);
	}
}
